{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once installed, you need to load the `pytorch_memlab` IPython extensions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext pytorch_memlab"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One magic is provided, `mlrun` which can act either as a line magic `%mlrun`, or as a cell magic `%%mlrun`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%mlrun?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we need some torch code to profile:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "def x():\n",
    "    torch.nn.Linear(100, 100).cuda()\n",
    "    \n",
    "def y(gpu=0):\n",
    "    torch.nn.Linear(1000, 100).cuda(device=gpu)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can profile multiple functions at the same type by repeatedly specifying `-f`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h3><span style=\"font-family: monospace\">x</span></h3><div><style  type=\"text/css\" >\n",
       "    #T_3e650110_8557_11ea_91ba_14187765e3c9 th {\n",
       "          text-align: left;\n",
       "    }    #T_3e650110_8557_11ea_91ba_14187765e3c9row0_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3e650110_8557_11ea_91ba_14187765e3c9row0_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3e650110_8557_11ea_91ba_14187765e3c9row0_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }    #T_3e650110_8557_11ea_91ba_14187765e3c9row1_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#4878d0 10.1%, transparent 10.1%);\n",
       "        }    #T_3e650110_8557_11ea_91ba_14187765e3c9row1_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#ee854a 99.0%, transparent 99.0%);\n",
       "        }    #T_3e650110_8557_11ea_91ba_14187765e3c9row1_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }</style><table id=\"T_3e650110_8557_11ea_91ba_14187765e3c9\" ><thead>    <tr>        <th class=\"col_heading level0 col0\" >active_bytes</th>        <th class=\"col_heading level0 col1\" >reserved_bytes</th>        <th class=\"col_heading level0 col2\" >line</th>        <th class=\"col_heading level0 col3\" >code</th>    </tr>    <tr>        <th class=\"col_heading level1 col0\" >all</th>        <th class=\"col_heading level1 col1\" >all</th>        <th class=\"col_heading level1 col2\" ></th>        <th class=\"col_heading level1 col3\" ></th>    </tr>    <tr>        <th class=\"col_heading level2 col0\" >peak</th>        <th class=\"col_heading level2 col1\" >peak</th>        <th class=\"col_heading level2 col2\" ></th>        <th class=\"col_heading level2 col3\" ></th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                                <td id=\"T_3e650110_8557_11ea_91ba_14187765e3c9row0_col0\" class=\"data row0 col0\" >0.00B</td>\n",
       "                        <td id=\"T_3e650110_8557_11ea_91ba_14187765e3c9row0_col1\" class=\"data row0 col1\" >0.00B</td>\n",
       "                        <td id=\"T_3e650110_8557_11ea_91ba_14187765e3c9row0_col2\" class=\"data row0 col2\" >3</td>\n",
       "                        <td id=\"T_3e650110_8557_11ea_91ba_14187765e3c9row0_col3\" class=\"data row0 col3\" >def x():\n",
       "</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                                <td id=\"T_3e650110_8557_11ea_91ba_14187765e3c9row1_col0\" class=\"data row1 col0\" >40.00K</td>\n",
       "                        <td id=\"T_3e650110_8557_11ea_91ba_14187765e3c9row1_col1\" class=\"data row1 col1\" >2.00M</td>\n",
       "                        <td id=\"T_3e650110_8557_11ea_91ba_14187765e3c9row1_col2\" class=\"data row1 col2\" >4</td>\n",
       "                        <td id=\"T_3e650110_8557_11ea_91ba_14187765e3c9row1_col3\" class=\"data row1 col3\" >    torch.nn.Linear(100, 100).cuda()\n",
       "</td>\n",
       "            </tr>\n",
       "    </tbody></table></div>\n",
       "<h3><span style=\"font-family: monospace\">y</span></h3><div><style  type=\"text/css\" >\n",
       "    #T_3e6e2fd8_8557_11ea_91ba_14187765e3c9 th {\n",
       "          text-align: left;\n",
       "    }    #T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row0_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row0_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#ee854a 99.0%, transparent 99.0%);\n",
       "        }    #T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row0_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }    #T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row1_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#4878d0 99.0%, transparent 99.0%);\n",
       "        }    #T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row1_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#ee854a 99.0%, transparent 99.0%);\n",
       "        }    #T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row1_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }</style><table id=\"T_3e6e2fd8_8557_11ea_91ba_14187765e3c9\" ><thead>    <tr>        <th class=\"col_heading level0 col0\" >active_bytes</th>        <th class=\"col_heading level0 col1\" >reserved_bytes</th>        <th class=\"col_heading level0 col2\" >line</th>        <th class=\"col_heading level0 col3\" >code</th>    </tr>    <tr>        <th class=\"col_heading level1 col0\" >all</th>        <th class=\"col_heading level1 col1\" >all</th>        <th class=\"col_heading level1 col2\" ></th>        <th class=\"col_heading level1 col3\" ></th>    </tr>    <tr>        <th class=\"col_heading level2 col0\" >peak</th>        <th class=\"col_heading level2 col1\" >peak</th>        <th class=\"col_heading level2 col2\" ></th>        <th class=\"col_heading level2 col3\" ></th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                                <td id=\"T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row0_col0\" class=\"data row0 col0\" >0.00B</td>\n",
       "                        <td id=\"T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row0_col1\" class=\"data row0 col1\" >2.00M</td>\n",
       "                        <td id=\"T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row0_col2\" class=\"data row0 col2\" >6</td>\n",
       "                        <td id=\"T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row0_col3\" class=\"data row0 col3\" >def y(gpu=0):\n",
       "</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                                <td id=\"T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row1_col0\" class=\"data row1 col0\" >391.50K</td>\n",
       "                        <td id=\"T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row1_col1\" class=\"data row1 col1\" >2.00M</td>\n",
       "                        <td id=\"T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row1_col2\" class=\"data row1 col2\" >7</td>\n",
       "                        <td id=\"T_3e6e2fd8_8557_11ea_91ba_14187765e3c9row1_col3\" class=\"data row1 col3\" >    torch.nn.Linear(1000, 100).cuda(device=gpu)\n",
       "</td>\n",
       "            </tr>\n",
       "    </tbody></table></div>"
      ],
      "text/plain": [
       "## x\n",
       "\n",
       "active_bytes reserved_bytes line  code                                 \n",
       "         all            all                                            \n",
       "        peak           peak                                            \n",
       "       0.00B          0.00B    3  def x():                             \n",
       "      40.00K          2.00M    4      torch.nn.Linear(100, 100).cuda() \n",
       "\n",
       "\n",
       "## y\n",
       "\n",
       "active_bytes reserved_bytes line  code                                            \n",
       "         all            all                                                       \n",
       "        peak           peak                                                       \n",
       "       0.00B          2.00M    6  def y(gpu=0):                                   \n",
       "     391.50K          2.00M    7      torch.nn.Linear(1000, 100).cuda(device=gpu) "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%mlrun -f x -f y\n",
    "\n",
    "x()\n",
    "y()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can alos profile with the `%mlrun` line magic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h3><span style=\"font-family: monospace\">z</span></h3><div><style  type=\"text/css\" >\n",
       "    #T_3e870152_8557_11ea_91ba_14187765e3c9 th {\n",
       "          text-align: left;\n",
       "    }    #T_3e870152_8557_11ea_91ba_14187765e3c9row0_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3e870152_8557_11ea_91ba_14187765e3c9row0_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3e870152_8557_11ea_91ba_14187765e3c9row0_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }    #T_3e870152_8557_11ea_91ba_14187765e3c9row1_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#4878d0 99.0%, transparent 99.0%);\n",
       "        }    #T_3e870152_8557_11ea_91ba_14187765e3c9row1_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#ee854a 99.0%, transparent 99.0%);\n",
       "        }    #T_3e870152_8557_11ea_91ba_14187765e3c9row1_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }</style><table id=\"T_3e870152_8557_11ea_91ba_14187765e3c9\" ><thead>    <tr>        <th class=\"col_heading level0 col0\" >active_bytes</th>        <th class=\"col_heading level0 col1\" >reserved_bytes</th>        <th class=\"col_heading level0 col2\" >line</th>        <th class=\"col_heading level0 col3\" >code</th>    </tr>    <tr>        <th class=\"col_heading level1 col0\" >all</th>        <th class=\"col_heading level1 col1\" >all</th>        <th class=\"col_heading level1 col2\" ></th>        <th class=\"col_heading level1 col3\" ></th>    </tr>    <tr>        <th class=\"col_heading level2 col0\" >peak</th>        <th class=\"col_heading level2 col1\" >peak</th>        <th class=\"col_heading level2 col2\" ></th>        <th class=\"col_heading level2 col3\" ></th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                                <td id=\"T_3e870152_8557_11ea_91ba_14187765e3c9row0_col0\" class=\"data row0 col0\" >0.00B</td>\n",
       "                        <td id=\"T_3e870152_8557_11ea_91ba_14187765e3c9row0_col1\" class=\"data row0 col1\" >0.00B</td>\n",
       "                        <td id=\"T_3e870152_8557_11ea_91ba_14187765e3c9row0_col2\" class=\"data row0 col2\" >1</td>\n",
       "                        <td id=\"T_3e870152_8557_11ea_91ba_14187765e3c9row0_col3\" class=\"data row0 col3\" >def z():\n",
       "</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                                <td id=\"T_3e870152_8557_11ea_91ba_14187765e3c9row1_col0\" class=\"data row1 col0\" >40.00K</td>\n",
       "                        <td id=\"T_3e870152_8557_11ea_91ba_14187765e3c9row1_col1\" class=\"data row1 col1\" >2.00M</td>\n",
       "                        <td id=\"T_3e870152_8557_11ea_91ba_14187765e3c9row1_col2\" class=\"data row1 col2\" >2</td>\n",
       "                        <td id=\"T_3e870152_8557_11ea_91ba_14187765e3c9row1_col3\" class=\"data row1 col3\" >    torch.nn.Linear(100, 100).cuda()\n",
       "</td>\n",
       "            </tr>\n",
       "    </tbody></table></div>"
      ],
      "text/plain": [
       "## z\n",
       "\n",
       "active_bytes reserved_bytes line  code                                 \n",
       "         all            all                                            \n",
       "        peak           peak                                            \n",
       "       0.00B          0.00B    1  def z():                             \n",
       "      40.00K          2.00M    2      torch.nn.Linear(100, 100).cuda() "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def z():\n",
    "    torch.nn.Linear(100, 100).cuda()\n",
    "%mlrun -f z z()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can specify which GPU you wish to profile using `-g`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h3><span style=\"font-family: monospace\">x</span></h3><div><style  type=\"text/css\" >\n",
       "    #T_3ea0c4ac_8557_11ea_91ba_14187765e3c9 th {\n",
       "          text-align: left;\n",
       "    }    #T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row0_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row0_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row0_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }    #T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row1_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#4878d0 10.1%, transparent 10.1%);\n",
       "        }    #T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row1_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#ee854a 99.0%, transparent 99.0%);\n",
       "        }    #T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row1_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }</style><table id=\"T_3ea0c4ac_8557_11ea_91ba_14187765e3c9\" ><thead>    <tr>        <th class=\"col_heading level0 col0\" >active_bytes</th>        <th class=\"col_heading level0 col1\" >reserved_bytes</th>        <th class=\"col_heading level0 col2\" >line</th>        <th class=\"col_heading level0 col3\" >code</th>    </tr>    <tr>        <th class=\"col_heading level1 col0\" >all</th>        <th class=\"col_heading level1 col1\" >all</th>        <th class=\"col_heading level1 col2\" ></th>        <th class=\"col_heading level1 col3\" ></th>    </tr>    <tr>        <th class=\"col_heading level2 col0\" >peak</th>        <th class=\"col_heading level2 col1\" >peak</th>        <th class=\"col_heading level2 col2\" ></th>        <th class=\"col_heading level2 col3\" ></th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                                <td id=\"T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row0_col0\" class=\"data row0 col0\" >0.00B</td>\n",
       "                        <td id=\"T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row0_col1\" class=\"data row0 col1\" >0.00B</td>\n",
       "                        <td id=\"T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row0_col2\" class=\"data row0 col2\" >3</td>\n",
       "                        <td id=\"T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row0_col3\" class=\"data row0 col3\" >def x():\n",
       "</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                                <td id=\"T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row1_col0\" class=\"data row1 col0\" >40.00K</td>\n",
       "                        <td id=\"T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row1_col1\" class=\"data row1 col1\" >2.00M</td>\n",
       "                        <td id=\"T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row1_col2\" class=\"data row1 col2\" >4</td>\n",
       "                        <td id=\"T_3ea0c4ac_8557_11ea_91ba_14187765e3c9row1_col3\" class=\"data row1 col3\" >    torch.nn.Linear(100, 100).cuda()\n",
       "</td>\n",
       "            </tr>\n",
       "    </tbody></table></div>\n",
       "<h3><span style=\"font-family: monospace\">y</span></h3><div><style  type=\"text/css\" >\n",
       "    #T_3ea87986_8557_11ea_91ba_14187765e3c9 th {\n",
       "          text-align: left;\n",
       "    }    #T_3ea87986_8557_11ea_91ba_14187765e3c9row0_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3ea87986_8557_11ea_91ba_14187765e3c9row0_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#ee854a 99.0%, transparent 99.0%);\n",
       "        }    #T_3ea87986_8557_11ea_91ba_14187765e3c9row0_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }    #T_3ea87986_8557_11ea_91ba_14187765e3c9row1_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#4878d0 99.0%, transparent 99.0%);\n",
       "        }    #T_3ea87986_8557_11ea_91ba_14187765e3c9row1_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#ee854a 99.0%, transparent 99.0%);\n",
       "        }    #T_3ea87986_8557_11ea_91ba_14187765e3c9row1_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }</style><table id=\"T_3ea87986_8557_11ea_91ba_14187765e3c9\" ><thead>    <tr>        <th class=\"col_heading level0 col0\" >active_bytes</th>        <th class=\"col_heading level0 col1\" >reserved_bytes</th>        <th class=\"col_heading level0 col2\" >line</th>        <th class=\"col_heading level0 col3\" >code</th>    </tr>    <tr>        <th class=\"col_heading level1 col0\" >all</th>        <th class=\"col_heading level1 col1\" >all</th>        <th class=\"col_heading level1 col2\" ></th>        <th class=\"col_heading level1 col3\" ></th>    </tr>    <tr>        <th class=\"col_heading level2 col0\" >peak</th>        <th class=\"col_heading level2 col1\" >peak</th>        <th class=\"col_heading level2 col2\" ></th>        <th class=\"col_heading level2 col3\" ></th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                                <td id=\"T_3ea87986_8557_11ea_91ba_14187765e3c9row0_col0\" class=\"data row0 col0\" >0.00B</td>\n",
       "                        <td id=\"T_3ea87986_8557_11ea_91ba_14187765e3c9row0_col1\" class=\"data row0 col1\" >2.00M</td>\n",
       "                        <td id=\"T_3ea87986_8557_11ea_91ba_14187765e3c9row0_col2\" class=\"data row0 col2\" >6</td>\n",
       "                        <td id=\"T_3ea87986_8557_11ea_91ba_14187765e3c9row0_col3\" class=\"data row0 col3\" >def y(gpu=0):\n",
       "</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                                <td id=\"T_3ea87986_8557_11ea_91ba_14187765e3c9row1_col0\" class=\"data row1 col0\" >391.50K</td>\n",
       "                        <td id=\"T_3ea87986_8557_11ea_91ba_14187765e3c9row1_col1\" class=\"data row1 col1\" >2.00M</td>\n",
       "                        <td id=\"T_3ea87986_8557_11ea_91ba_14187765e3c9row1_col2\" class=\"data row1 col2\" >7</td>\n",
       "                        <td id=\"T_3ea87986_8557_11ea_91ba_14187765e3c9row1_col3\" class=\"data row1 col3\" >    torch.nn.Linear(1000, 100).cuda(device=gpu)\n",
       "</td>\n",
       "            </tr>\n",
       "    </tbody></table></div>"
      ],
      "text/plain": [
       "## x\n",
       "\n",
       "active_bytes reserved_bytes line  code                                 \n",
       "         all            all                                            \n",
       "        peak           peak                                            \n",
       "       0.00B          0.00B    3  def x():                             \n",
       "      40.00K          2.00M    4      torch.nn.Linear(100, 100).cuda() \n",
       "\n",
       "\n",
       "## y\n",
       "\n",
       "active_bytes reserved_bytes line  code                                            \n",
       "         all            all                                                       \n",
       "        peak           peak                                                       \n",
       "       0.00B          2.00M    6  def y(gpu=0):                                   \n",
       "     391.50K          2.00M    7      torch.nn.Linear(1000, 100).cuda(device=gpu) "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%mlrun -f x -f y -g 0 y\n",
    "\n",
    "x()\n",
    "y(gpu=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can get a handle on the `LineProfiler` object using `-r`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h3><span style=\"font-family: monospace\">x</span></h3><div><style  type=\"text/css\" >\n",
       "    #T_3ec4a7f0_8557_11ea_91ba_14187765e3c9 th {\n",
       "          text-align: left;\n",
       "    }    #T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row0_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row0_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row0_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }    #T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row1_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#4878d0 99.0%, transparent 99.0%);\n",
       "        }    #T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row1_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#ee854a 99.0%, transparent 99.0%);\n",
       "        }    #T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row1_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }</style><table id=\"T_3ec4a7f0_8557_11ea_91ba_14187765e3c9\" ><thead>    <tr>        <th class=\"col_heading level0 col0\" >active_bytes</th>        <th class=\"col_heading level0 col1\" >reserved_bytes</th>        <th class=\"col_heading level0 col2\" >line</th>        <th class=\"col_heading level0 col3\" >code</th>    </tr>    <tr>        <th class=\"col_heading level1 col0\" >all</th>        <th class=\"col_heading level1 col1\" >all</th>        <th class=\"col_heading level1 col2\" ></th>        <th class=\"col_heading level1 col3\" ></th>    </tr>    <tr>        <th class=\"col_heading level2 col0\" >peak</th>        <th class=\"col_heading level2 col1\" >peak</th>        <th class=\"col_heading level2 col2\" ></th>        <th class=\"col_heading level2 col3\" ></th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                                <td id=\"T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row0_col0\" class=\"data row0 col0\" >0.00B</td>\n",
       "                        <td id=\"T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row0_col1\" class=\"data row0 col1\" >0.00B</td>\n",
       "                        <td id=\"T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row0_col2\" class=\"data row0 col2\" >3</td>\n",
       "                        <td id=\"T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row0_col3\" class=\"data row0 col3\" >def x():\n",
       "</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                                <td id=\"T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row1_col0\" class=\"data row1 col0\" >40.00K</td>\n",
       "                        <td id=\"T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row1_col1\" class=\"data row1 col1\" >2.00M</td>\n",
       "                        <td id=\"T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row1_col2\" class=\"data row1 col2\" >4</td>\n",
       "                        <td id=\"T_3ec4a7f0_8557_11ea_91ba_14187765e3c9row1_col3\" class=\"data row1 col3\" >    torch.nn.Linear(100, 100).cuda()\n",
       "</td>\n",
       "            </tr>\n",
       "    </tbody></table></div>"
      ],
      "text/plain": [
       "## x\n",
       "\n",
       "active_bytes reserved_bytes line  code                                 \n",
       "         all            all                                            \n",
       "        peak           peak                                            \n",
       "       0.00B          0.00B    3  def x():                             \n",
       "      40.00K          2.00M    4      torch.nn.Linear(100, 100).cuda() "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "profiler = %mlrun -q -r -f x x()\n",
    "profiler.display()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can dump stats out to a file using `-T`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h3><span style=\"font-family: monospace\">x</span></h3><div><style  type=\"text/css\" >\n",
       "    #T_3ef1ac96_8557_11ea_91ba_14187765e3c9 th {\n",
       "          text-align: left;\n",
       "    }    #T_3ef1ac96_8557_11ea_91ba_14187765e3c9row0_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3ef1ac96_8557_11ea_91ba_14187765e3c9row0_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "        }    #T_3ef1ac96_8557_11ea_91ba_14187765e3c9row0_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }    #T_3ef1ac96_8557_11ea_91ba_14187765e3c9row1_col0 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#4878d0 99.0%, transparent 99.0%);\n",
       "        }    #T_3ef1ac96_8557_11ea_91ba_14187765e3c9row1_col1 {\n",
       "            width:  10em;\n",
       "             height:  80%;\n",
       "            background:  linear-gradient(90deg,#ee854a 99.0%, transparent 99.0%);\n",
       "        }    #T_3ef1ac96_8557_11ea_91ba_14187765e3c9row1_col3 {\n",
       "            text-align:  left;\n",
       "            white-space:  pre;\n",
       "            font-family:  monospace;\n",
       "        }</style><table id=\"T_3ef1ac96_8557_11ea_91ba_14187765e3c9\" ><thead>    <tr>        <th class=\"col_heading level0 col0\" >active_bytes</th>        <th class=\"col_heading level0 col1\" >reserved_bytes</th>        <th class=\"col_heading level0 col2\" >line</th>        <th class=\"col_heading level0 col3\" >code</th>    </tr>    <tr>        <th class=\"col_heading level1 col0\" >all</th>        <th class=\"col_heading level1 col1\" >all</th>        <th class=\"col_heading level1 col2\" ></th>        <th class=\"col_heading level1 col3\" ></th>    </tr>    <tr>        <th class=\"col_heading level2 col0\" >peak</th>        <th class=\"col_heading level2 col1\" >peak</th>        <th class=\"col_heading level2 col2\" ></th>        <th class=\"col_heading level2 col3\" ></th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                                <td id=\"T_3ef1ac96_8557_11ea_91ba_14187765e3c9row0_col0\" class=\"data row0 col0\" >0.00B</td>\n",
       "                        <td id=\"T_3ef1ac96_8557_11ea_91ba_14187765e3c9row0_col1\" class=\"data row0 col1\" >0.00B</td>\n",
       "                        <td id=\"T_3ef1ac96_8557_11ea_91ba_14187765e3c9row0_col2\" class=\"data row0 col2\" >3</td>\n",
       "                        <td id=\"T_3ef1ac96_8557_11ea_91ba_14187765e3c9row0_col3\" class=\"data row0 col3\" >def x():\n",
       "</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                                <td id=\"T_3ef1ac96_8557_11ea_91ba_14187765e3c9row1_col0\" class=\"data row1 col0\" >40.00K</td>\n",
       "                        <td id=\"T_3ef1ac96_8557_11ea_91ba_14187765e3c9row1_col1\" class=\"data row1 col1\" >2.00M</td>\n",
       "                        <td id=\"T_3ef1ac96_8557_11ea_91ba_14187765e3c9row1_col2\" class=\"data row1 col2\" >4</td>\n",
       "                        <td id=\"T_3ef1ac96_8557_11ea_91ba_14187765e3c9row1_col3\" class=\"data row1 col3\" >    torch.nn.Linear(100, 100).cuda()\n",
       "</td>\n",
       "            </tr>\n",
       "    </tbody></table></div>"
      ],
      "text/plain": [
       "## x\n",
       "\n",
       "active_bytes reserved_bytes line  code                                 \n",
       "         all            all                                            \n",
       "        peak           peak                                            \n",
       "       0.00B          0.00B    3  def x():                             \n",
       "      40.00K          2.00M    4      torch.nn.Linear(100, 100).cuda() "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%mlrun -q -T profile.log -f x x()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "## x\r\n",
      "\r\n",
      "active_bytes reserved_bytes line  code                                 \r\n",
      "         all            all                                            \r\n",
      "        peak           peak                                            \r\n",
      "       0.00B          0.00B    3  def x():                             \r\n",
      "      40.00K          2.00M    4      torch.nn.Linear(100, 100).cuda() \r\n"
     ]
    }
   ],
   "source": [
    "!head profile.log"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
